package enrique.kurso.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentProviderOperation;
import android.content.Context;
import android.content.Intent;
import android.content.OperationApplicationException;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Intents.Insert;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.RawContacts.Data;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;



public class Utils {

	private static final String TAG = "Utils";


	public static void toast(Context context, String msg) {
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}

	/**
	 * Parsea el contenido de un JSON en un inputStream y lo devuelve como
	 * JSONObject
	 * 
	 * @param sourceFile
	 * @return JSONObject resultado de parsea el inputStream de entrada. En caso
	 *         de error devuelve null
	 */
	public static JSONObject getJSONFromStream(InputStream inputStream) {
		BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
		String aux = "";
		String jString = "";
		JSONObject result = null;
		try {
			while ((aux = bf.readLine()) != null) {
				jString = jString + aux;
			}
			result = new JSONObject(jString);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}


	public static Drawable getDrawableFromString(Context ctx, String drawableName) {
		Log.d(TAG,"buscando drawable:"+drawableName);
		int drawableID = ctx.getResources().getIdentifier(drawableName, "drawable", ctx.getPackageName());
		return ctx.getResources().getDrawable(drawableID);
	}

	public static String getStringFromStringName(Context ctx, String stringName) {
		if (stringName == null || stringName.length() == 0) {
			return "";
		}
		String str = null;
		// Busca la cadena en strings.xml.
		// Si no la encuentra devuelve la propia cadena pasada como par�metro
		// (stringName)
		try {
			int stringID = ctx.getResources().getIdentifier(stringName, "string", ctx.getPackageName());
			str = ctx.getResources().getString(stringID);
		} catch (Resources.NotFoundException e) {
			str = stringName;
		}
		return str;
	}

	
	
}

